package com.tanhua.admin.controller;

import com.tanhua.admin.service.ManageService;
import com.tanhua.model.vo.MovementsVo;
import com.tanhua.model.vo.PageResult;
import com.tanhua.model.vo.UserInfoVoAge;
import org.bson.types.ObjectId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/manage")
public class ManageController {

    @Autowired
    private ManageService manageService;

    /**
     * 分页查询用户
     * @param page
     * @param pageSize
     * @return
     */
    @GetMapping("/users")
    public ResponseEntity findUserPage(@RequestParam(defaultValue = "1")Long page,
                                   @RequestParam(name = "pagesize", defaultValue = "10") Long pageSize) {
        PageResult<UserInfoVoAge> pageResult = manageService.findUserPage(page, pageSize);
        return ResponseEntity.ok(pageResult);
    }

    /**
     * 查询用户详情
     * @return
     */
    @GetMapping("/users/{userId}")
    public ResponseEntity getUserInfo(@PathVariable Long userId){
        UserInfoVoAge vo = manageService.findById(userId);
        return ResponseEntity.ok(vo);
    }

    /**
     * 查询指定用户发布的所有视频列表
     */
    @GetMapping("/videos")
    public ResponseEntity videos(@RequestParam(defaultValue = "1") Long page,
                                 @RequestParam(defaultValue = "10", name = "pagesize") Long pageSize,
                                 Long uid ) {
        PageResult result = manageService.findAllVideos(page,pageSize,uid);
        return ResponseEntity.ok(result);
    }

    //查询动态
    @GetMapping("/messages")
    public ResponseEntity messages(@RequestParam(defaultValue = "1") Long page,
                                   @RequestParam(defaultValue = "10") Long pagesize,
                                   Long uid,String state) {
        Integer movementState = null;
        if(!"".equals(state) && !"''".equals(state) && null != state) {
             movementState = Integer.parseInt(state);
        }
        PageResult result = manageService.findAllMovements(page,pagesize,uid,movementState);
        return ResponseEntity.ok(result);
    }

    /**
     * 用户冻结
     * @param params
     * @return
     */
    @PostMapping("/users/freeze")
    public ResponseEntity userFreeze(@RequestBody Map<String, String> params) {
        Map<String,String> resultMap = manageService.userFreeze(params);
        return ResponseEntity.ok(resultMap);
    }

    /**
     * 用户解冻
     * @param paramMap
     * @return
     */
    @PostMapping("/users/unfreeze")
    public ResponseEntity unfreezeUser(@RequestBody Map<String,String> paramMap){
        Long userId = Long.valueOf(paramMap.get("userId"));
        String reasonForThawing = paramMap.get("reasonForThawing");
        Map<String, String> resultMap = manageService.unfreezeUser(userId, reasonForThawing);
        return ResponseEntity.ok(resultMap);
    }

    @GetMapping("/messages/{id}")
    public ResponseEntity movementDetail(@PathVariable String id) {
        MovementsVo vo = manageService.movementDetail(id);
        return ResponseEntity.ok(vo);
    }

    /**
     *
     * @return
     */
    @GetMapping("/messages/comments")
    public ResponseEntity getComments(@RequestParam String messageID,
                                      @RequestParam(defaultValue = "1")Long page,
                                      @RequestParam(defaultValue = "10", name = "pagesize")Long pageSize) {
        PageResult pageResult = manageService.getComment(messageID, page, pageSize);
        return ResponseEntity.ok(pageResult);
    }

    @PostMapping("/messages/pass")
    public ResponseEntity passMovement(@RequestBody List<ObjectId> movementIds) {
        Map<String,String> resultMap = manageService.passMovement(movementIds);
        return ResponseEntity.ok(resultMap);
    }

    @PostMapping("/messages/reject")
    public ResponseEntity rejectMovement(@RequestBody List<ObjectId> movementIds) {
        Map<String,String> resultMap = manageService.rejectMovement(movementIds);
        return ResponseEntity.ok(resultMap);
    }

}
